package cn.txun05.csmall.product.preload;

import cn.txun05.csmall.product.cache.IAlbumCacheRepository;
import cn.txun05.csmall.product.mapper.AlbumMapper;
import cn.txun05.csmall.product.pojo.vo.AlbumListItemVo;
import cn.txun05.csmall.product.pojo.vo.AlbumStandardVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class CachePreload implements ApplicationRunner {
    @Autowired
    private IAlbumCacheRepository albumCacheRepository;
    @Autowired
    private AlbumMapper albumMapper;
    //此方法会在项目启动完毕后，自动执行
    @Override
    public void run(ApplicationArguments args) throws Exception {
        List<AlbumListItemVo> list = albumMapper.list();
        for(AlbumListItemVo albumListItemVo:list){
            AlbumStandardVo albumStandardVo = new AlbumStandardVo();
            BeanUtils.copyProperties(albumListItemVo,albumStandardVo);
            albumCacheRepository.save(albumStandardVo);
        }
    }
}
